import 'dart:developer';

import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:native_dart_package/http/request/exception/api_exception.dart';
import 'package:native_dart_package/http/request/exception/exception_handler.dart';
import 'package:native_dart_package/native_dart_package.dart';
import 'package:reactive_forms/reactive_forms.dart';
import 'package:sprintf/sprintf.dart';
import 'package:yt_devops/configs.dart';
import 'package:yt_devops/pages/devices/device_tab/basic_info/setting/setting_form/setting_form_widget.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/services/device/device_sevices.dart';

import 'setting_form/setting_form_model.dart';

class Rect100WSetting {
  final int productId;

  Rect100WSetting(
    this.productId,
  );

  GlobalKey<SettingFormWidgetState> _formKey = GlobalKey();

  DeviceServices _service = DeviceServices();

  List<SettingFormModel> get form => [
        SettingFormModel(
          key: 'projectId',
          title: '${ResKey.deviceItem.tr} ID',
          formItemProps: FormItemProps(
            required: true,
          ),
        ),
        SettingFormModel(
          key: 'bmsId',
          title: 'BMS ID',
          formItemProps: FormItemProps(
            required: true,
          ),
        ),
        SettingFormModel(
          key: 'emsId',
          title: 'EMS  ID',
          formItemProps: FormItemProps(
            required: true,
          ),
        ),
        SettingFormModel(
          key: 'pcsId',
          title: 'PCS ID',
          formItemProps: FormItemProps(
            required: true,
          ),
        ),
        SettingFormModel(
          key: 'temperatureDeviceId',
          title: '${ResKey.deviceCellTemperature.tr} ID',
          formItemProps: FormItemProps(
            required: true,
          ),
        ),
        SettingFormModel(
          key: 'voltageDeviceId',
          title: '${ResKey.deviceCellVoltage.tr} ID',
          formItemProps: FormItemProps(
            required: true,
          ),
        ),
        SettingFormModel(
          key: 'airConditionerId',
          title: '${ResKey.deviceAirConditioner.tr} ID',
          formItemProps: FormItemProps(
            required: true,
          ),
        ),
        SettingFormModel(
          key: 'clockDeviceId',
          title: '${ResKey.deviceSystemClock.tr}',
        ),
        SettingFormModel(
          key: 'statisticsDeviceId',
          title: '${ResKey.deviceEnergyConsumptionStatistics.tr.tr} ID',
        ),
        SettingFormModel(
          key: 'thirdSiteName',
          title: ResKey.thirdPartySite.tr,
          valueType: FormItemType.selectPicker,
          request: getThirdSite,
          formItemProps: FormItemProps(
            required: true,
            onSelectChange: (Map data, OptionModel option) =>
                data['thirdSiteId'] = option.value,
          ),
        ),
        SettingFormModel(
          key: 'powerGridSideId',
          title: ResKey.deviceGridMeter.tr,
        ),
        SettingFormModel(
          key: 'inverterSideId',
          title: ResKey.deviceInverterMeter.tr,
        ),
        SettingFormModel(
          key: 'thirdSiteId',
          title: '',
          formItemProps: FormItemProps(hidden: true),
        ),
      ];

  Future<List<OptionModel>> getThirdSite(Map data) async {
    showLoading(toast: ResKey.loading.tr);

    try {
      var data = await _service.getThirdSiteList(
          {'current': 1, 'pageSize': 10000, 'productId': productId});
      List thirdSiteList = data['list'];
      return thirdSiteList
          .map<OptionModel>((e) => OptionModel(e['siteName'], e['id']))
          .toList();
    } on ApiException catch (e) {
      handleException(e);
      return [];
    } finally {
      dismissLoading();
    }
  }
}
